package com.youkeyi.ddy.cloud.application.system.ad.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.ad.SystemAdCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.ad.dto.*;
import com.youkeyi.ddy.cloud.common.api.application.system.ad.param.SystemAdAreaListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.ad.param.SystemAdCityListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.ad.param.SystemAdProvinceListCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.PublicAdCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.dto.PublicAdAreaListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.dto.PublicAdCityListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.dto.PublicAdProvinceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.param.PublicAdAreaQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.param.PublicAdCityQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.param.PublicAdProvinceQueryCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * SystemAdController
 *
 * @Author liuxiawang
 * @Date 2024/4/1 10:51 AM
 * @Location shenzhen.china
 */
@Service
public class SystemAdController implements SystemAdCloudService {

    @Autowired
    private PublicAdCloudService publicAdCloudService;

    @Override
    public SystemAdProvinceListCloudDto provinceList(SystemAdProvinceListCloudParam param) {
        SystemAdProvinceListCloudDto dto = new SystemAdProvinceListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicAdProvinceQueryCloudParam provinceQueryCloudParam = new PublicAdProvinceQueryCloudParam();
        provinceQueryCloudParam.setProvinceCodes(param.getProvinceCodes());
        provinceQueryCloudParam.setProvinceNameFuzzy(param.getProvinceNameFuzzy());
        provinceQueryCloudParam.setPageSize(param.getPageSize());
        provinceQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicAdProvinceListCloudDto provinceDtos = publicAdCloudService.queryProvince(provinceQueryCloudParam);
        if (null != provinceDtos && CollectionUtils.isNotEmpty(provinceDtos.getRows())) {
            List<SystemAdProvinceListItemCloudDto> items = new ArrayList<>();
            provinceDtos.getRows().forEach(province -> {
                SystemAdProvinceListItemCloudDto item = new SystemAdProvinceListItemCloudDto();
                item.setProvinceId(province.getProvinceId());
                item.setProvinceCode(province.getProvinceCode());
                item.setProvinceName(province.getProvinceName());
                item.setShortName(province.getShortName());
                item.setLng(province.getLng());
                item.setLat(province.getLat());
                item.setSort(province.getSort());
                item.setGmtCreate(province.getGmtCreate());
                item.setGmtModified(province.getGmtModified());
                item.setMemo(province.getMemo());
                item.setDataState(province.getDataState());
                item.setTenantCode(province.getTenantCode());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(provinceDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemAdCityListCloudDto cityList(SystemAdCityListCloudParam param) {
        SystemAdCityListCloudDto dto = new SystemAdCityListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicAdCityQueryCloudParam cityQueryCloudParam = new PublicAdCityQueryCloudParam();
        cityQueryCloudParam.setCityCodes(param.getCityCodes());
        cityQueryCloudParam.setCityNameFuzzy(param.getCityNameFuzzy());
        cityQueryCloudParam.setProvinceCodes(param.getProvinceCodes());
        cityQueryCloudParam.setPageSize(param.getPageSize());
        cityQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicAdCityListCloudDto cityDtos = publicAdCloudService.queryCity(cityQueryCloudParam);
        if (null != cityDtos && CollectionUtils.isNotEmpty(cityDtos.getRows())) {
            List<SystemAdCityListItemCloudDto> items = new ArrayList<>();
            cityDtos.getRows().forEach(city -> {
                SystemAdCityListItemCloudDto item = new SystemAdCityListItemCloudDto();
                item.setCityId(city.getCityId());
                item.setCityCode(city.getCityCode());
                item.setCityName(city.getCityName());
                item.setProvinceCode(city.getProvinceCode());
                item.setShortName(city.getShortName());
                item.setLng(city.getLng());
                item.setLat(city.getLat());
                item.setSort(city.getSort());
                item.setGmtCreate(city.getGmtCreate());
                item.setGmtModified(city.getGmtModified());
                item.setMemo(city.getMemo());
                item.setDataState(city.getDataState());
                item.setTenantCode(city.getTenantCode());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(cityDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemAdAreaListCloudDto areaList(SystemAdAreaListCloudParam param) {
        SystemAdAreaListCloudDto dto = new SystemAdAreaListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicAdAreaQueryCloudParam areaQueryCloudParam = new PublicAdAreaQueryCloudParam();
        areaQueryCloudParam.setCityCodes(param.getCityCodes());
        areaQueryCloudParam.setAreaCodes(param.getAreaCodes());
        areaQueryCloudParam.setAreaNameFuzzy(param.getAreaNameFuzzy());
        areaQueryCloudParam.setPageSize(param.getPageSize());
        areaQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicAdAreaListCloudDto areaDtos = publicAdCloudService.queryArea(areaQueryCloudParam);
        if (null != areaDtos && CollectionUtils.isNotEmpty(areaDtos.getRows())) {
            List<SystemAdAreaListItemCloudDto> items = new ArrayList<>();
            areaDtos.getRows().forEach(area -> {
                SystemAdAreaListItemCloudDto item = new SystemAdAreaListItemCloudDto();
                item.setAreaId(area.getAreaId());
                item.setAreaCode(area.getAreaCode());
                item.setCityCode(area.getCityCode());
                item.setAreaName(area.getAreaName());
                item.setShortName(area.getShortName());
                item.setLng(area.getLng());
                item.setLat(area.getLat());
                item.setSort(area.getSort());
                item.setGmtCreate(area.getGmtCreate());
                item.setGmtModified(area.getGmtModified());
                item.setMemo(area.getMemo());
                item.setDataState(area.getDataState());
                item.setTenantCode(area.getTenantCode());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(areaDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }
}
